/* ClientOperator
 * manages the client's view of the game taking place on the server
 * takes updates from the server and changes its table along with the server's
 * also handles client commands (GiveTurn)
 */

package sbcc.edu.blackjack.client;

import sbcc.edu.blackjack.GameState;
import sbcc.edu.blackjack.MainWindow;
import sbcc.edu.blackjack.Player;
import sbcc.edu.blackjack.command.client.ClientCommand;
import sbcc.edu.blackjack.command.server.GetTableState;
import sbcc.edu.blackjack.command.table.BetCommand;
import sbcc.edu.blackjack.command.table.TableCommand;

public class ClientOperator implements Runnable {
	ClientTable table;
	private GameState myState;
	private Player remotePlayer;
	private boolean runningClient;
	
	public ClientOperator(Player p) {
		table = new ClientTable();
		remotePlayer = p;
		runningClient = true;
	}
	
	public void run() {
		while(runningClient) {
			//TODO synchronize the table
			remotePlayer.sendCommand(new GetTableState());
			
			
			//if its our turn, send a command to the server
			if (remotePlayer.getState() == GameState.BETTING){
				//should not wait for input
				//TODO get bet from user
				MainWindow.setBettingVisible();
				int amt = 0;
				remotePlayer.sendCommand(new BetCommand(amt));
			}
			if (remotePlayer.getState() == GameState.PLAYING){	//server expets us to send back a TableCommand
				//TODO display options to user
				MainWindow.setGameButtonsVisible(false);
				//TODO get corresponding command
				//TODO wait for user to make a choice
				TableCommand tc = null;
				remotePlayer.sendCommand(tc);
			}
			//get next client command regardless of state
			ClientCommand received = remotePlayer.getClientCommand();
			if (received != null) {
			received.setClientOperator(this);
			received.execute();
			}
		}
	}

	public void quit(){
		runningClient = false;
	}

	public void sync(ClientTable changed) {
		// TODO Auto-generated method stub
		table = changed;
	}
}
